package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.MS;
import com.example.entity.Post;
import com.example.entity.PostTypeList;
import com.example.entity.Recruitment;
import com.example.service.PostService;
import javafx.geometry.Pos;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("posts")
public class PostController {

    @Autowired
    private PostService postService;


    /**
     * 岗位分页查询
     * @return
     */
    @GetMapping("/getPagePost/{current}/{size}")
    public R getPagePost(@PathVariable int current,@PathVariable int size,Post post){

        IPage page =new Page(current,size);
        LambdaQueryWrapper<Post> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(post.getCommercialId()),Post::getCommercialId,post.getCommercialId());
        lqw.like(Strings.isNotEmpty(post.getJobName()),Post::getJobName,post.getJobName());


        IPage page1 = postService.page(page,lqw);

        return R.success("岗位分页查询！",page1);
    }

    /**
     * 查询所有推荐岗位
     * @return
     */
    @GetMapping("/getAllPostByIsRecommend")
    public R getAllPostByIsRecommend(Post post){

        LambdaQueryWrapper<Post> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Post::getIsRecommend,"true");

        List<Post> list = postService.list(lqw);

        return R.success("查询所有推荐岗位！",list);
    }

    /**
     * 按岗位类型查询
     * @return
     */
    @GetMapping("/getAllPostByjobType")
    public R getAllPostByjobType(){

        ArrayList<PostTypeList> arr =new ArrayList<>();

        //查询通过审核的岗位分类

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status_application","true");
        queryWrapper.select("DISTINCT job_type");
        List<Post> list =postService.list(queryWrapper);


        //根据包含的分类 对包含的分类进行逐一查询对应分类的岗位
        for (Post post:list) {


            LambdaQueryWrapper<Post> lqw2 =new LambdaQueryWrapper<>();
            lqw2.eq(Strings.isNotEmpty(post.getJobType()),Post::getJobType,post.getJobType());
            List<Post> list1 = postService.list(lqw2);

            PostTypeList postTypeList =new PostTypeList();
            postTypeList.setJobType(post.getJobType());
            postTypeList.setPostList(list1);

            arr.add(postTypeList);
        }
        System.out.println(arr);

        return R.success("按岗位类型查询！",arr);
    }

    /**
     * 添加岗位
     * @return
     */
    @PostMapping("/addPost")
    public R addPost(@RequestBody Post post) {


        LambdaQueryWrapper<Post> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Strings.isNotEmpty(post.getJobName()), Post::getJobName, post.getJobName());

        //1.查询数据库此岗位名称是否存在
        List<Post> list = postService.list(lqw);

        if (list.size() == 0) {
            //2.不存在 添加
            boolean save = postService.save(post);
            if (save){
                return R.success("添加岗位成功！",null);
            }else {
                return R.error("添加岗位失败！");
            }
        }else {
            //3.存在
            return R.error("岗位名称存在！添加失败！");
        }
    }

    /**
     * 通过id查询岗位
     * @return
     */
    @GetMapping("/getPostById")
    public R getPostById(@PathVariable int id){

        LambdaQueryWrapper<Post> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Post::getId,id);
        Post one = postService.getOne(lqw);
        return R.success("通过id查询岗位",one);
    }

    /**
     * 通过id修改岗位
     * @return
     */
    @PutMapping("/updatePost")
    public R updatePost(@RequestBody Post post){


        boolean b = postService.updateById(post);
        if (b){
           return R.success("通过id修改岗位成功",post);
        }else {
           return R.error("通过id修改岗位失败！");
        }
    }

    /**
     * 通过id删除岗位
     * @return
     */
    @DeleteMapping("/deletePost/{id}")
    public R deletePost(@PathVariable int id){

        boolean b = postService.removeById(id);
        if (b){
           return R.success("通过id删除岗位成功",id);
        }else {
           return R.error("通过id删除岗位失败！");
        }
    }

    /**
     * 根据id查询岗位信息
     * @return
     */
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable int id){
        Post one = postService.getById(id);
        return R.success("根据id查询岗位信息(修改回显)",one);
    }


    /**
     * 通过id修改岗位申请状态
     * @return
     */
    @PutMapping("/updatePostStatus")
    public R updatePostStatus(@RequestBody Post post){
        boolean b = postService.updateById(post);
        if (b){
            return R.success("通过id修改岗位申请状态成功",post);
        }else {
            return R.error("通过id修改岗位申请状态失败！");
        }
    }

    /**
     * 通过id修改岗位招聘状态
     * @return
     */
//    @PutMapping("/updatePostStatus")
//    public R updatePostStatus(@RequestBody Recruitment recruitment){
//        boolean b = recruitmentService.updateById(recruitment);
//        if (b){
//            return R.success("通过id修改岗位招聘状态成功",recruitment);
//        }else {
//            return R.error("通过id修改岗位招聘状态失败！");
//        }
//    }



    /**
     * select major,COUNT(major) as count from student group by major ORDER BY count
     * 可视化（统计各个单位投放岗位的数量）
     */
    @GetMapping("/getRecruitmentByPost")
    public R getRecruitmentByPost(){

        List<MS> list = postService.getRecruitmentByPost();
        return R.success("统计各个单位投放岗位的数量",list);
    }

}

